<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/validate.jsp"%>
<%@ include file="function.jsp"%>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户管理</title>
    <link  href="../../hos_manage/images/favicon.ico?cache=<%=Math.random()%>" rel="shortcut icon" type="image/x-icon">
    <link rel="stylesheet" href="../css/public.css?cache=<%=Math.random()%>">
    <link rel="stylesheet" href="../css/bootstrap.css?cache=<%=Math.random()%>">
    <link rel="stylesheet" href="../css/messages_doctor_mes.css?cache=<%=Math.random()%>">
    <link rel="stylesheet" href="../css/sweetalert.css?cache=<%=Math.random()%>" >

    <script src="../js/jquery-1.9.1.min.js"></script>
</head>
<body>
    <%@ include file="../header.jsp"%>
    <!--内容-->
    <div class="containerone" id="user">
    <%@ include file="../left_menu.jsp"%>
        <!--右边内容-->
        <div class="right_cont">
            <!--右边导航下面内容-->
            <div>
                <div class="message_hos_news_container">
                    <!--左边的渠道-->
                    <div class="department_top">
                        <div class="department_top_left department_top_top">
                            <span>
                                <a href="role.jsp">
                                    <p class="padding-right-min user_back_bg">返回</p>
                                </a>
                             </span>
                        </div>
                        <div class="user_top_middle" v-html="roleName + title"></div>
                          <!--右边的搜索\添加\批量添加-->
                        <div class="department_top_right ui_line_height">
                           <%
                                if (checkAuth("添加后台管理员", input)) {
                            %>
                                <a href="add_user.jsp">
                                    <span class="add_btn doctor_add_btn " id='doctor_add_btn' @click="addUser()">添加</span>
                                </a>
                            <%
                                }
                            %>
                        </div>
                        <div class="clearfix"></div>
                    </div>
                    <div class="department_detail" id="doctor_detail">
                        <table class="department_detail_table">
                            <tr class='department_detail_tittle'>
                                <td class='ui_widthone'>登录名</td>
                                <td class='ui_widthone'>姓名</td>
                                <td class='ui_widthone'>创建人</td>
                                <td class='ui_widthone'>创建时间</td>
                                <td class='ui_widthone'>操作</td>
                            </tr>
                            <input type="hidden" name="deptCode" value="">
                            <tr class="department_detail_c" v-for="user in users">
                                <td v-html="user.user_name"></td>
                                <td v-html="user.name"></td>
                                <td v-html="user.operator_name"></td>
                                <td >
                                    <span class="delete_doc_mes" v-html="user.create_time"></span>
                                </td>
                                <td>
                                    <span class="mes_doc_edit doctor_add_btn">
                                        <%
                                            if (checkAuth("编辑后台管理员", input)) {
                                        %>
                                        <span class="href" @click="editUser(user)">编辑</a>
                                        <%
                                            }
                                        %>
                                    </span>
                                    <span class="mes_doc_edit doctor_add_btn delete_doc_mes" >
                                        <%
                                            if (checkAuth("删除后台管理员", input)) {
                                        %>
                                        <span class="href" @click="deleteUser(user)">删除</a>
                                        <%
                                            }
                                        %>
                                    </span>
                                    <span class="mes_doc_edit doctor_add_btn" data-toggle="modal" data-target="#myModal" @click="changeUser(user)">
                                        <%
                                            if (checkAuth("重置后台管理员密码", input)) {
                                        %>
                                        <a>重置密码</a>
                                        <%
                                            }
                                        %>
                                    </span>
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only"></span></button>
                        <h4 class="modal-title" id="myModalLabel">重置密码</h4>
                     </div>
                     <div class="modal-body">
                        <div class="modal-body_in" id="modal-body_list">
                            <div class="ui_password">设置密码
                                <input type="password" v-model="form.password" @input="vali('password')" @compositionstart="vali('password')" @compositionend="vali('password')"/>
                                <span class="test_tip" v-html="formVali.password"></span>
                            </div>
                            <div class="ui_password">重复密码
                                <input type="password" v-model="form.repassword" @input="vali('repassword')" @compositionstart="vali('repassword')" @compositionend="vali('repassword')"/>
                                <span class="test_tip" v-html="formVali.repassword"></span>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn_cancel" data-dismiss="modal">取消</button>
                        <button  class="btn btn_save" type="button" @click="resetPassword()">保存</button>
                    </div>
                </div>
            </div>
        </div>
    </div>

<script src="/js/sweetalert-dev.js"></script>
<script src="/js/vali.js"></script>
<script src="../js/modal.js"></script>
<script>
    new Vue({
        el: "#user",
        data: {
            title: "",
            role: {},
            roleName: "",
            users: [],
            nowUser: {},
            form: {
                password: "",
                repassword: "",
            },
            formTag: {
                password: false,
                repassword: false,
            },
            formVali: {
                password: "",
                repassword: "",
            }
        },
        computed: {
            valiResultPass: function() {
                for (var i in this.formVali) {
                    if (!this.formTag[i]) {
                        return false;
                    }
                }
                return true;
            }
        },
        methods: {
            changeUser: function(user) {
                this.nowUser = user;
                this.form = {
                    password: "",
                    repassword: "",
                };
                this.formTag = {
                    password: false,
                    repassword: false,
                };
                this.formVali = {
                    password: "",
                    repassword: "",
                };
            },
            vali: function(key) {
                if (key == "repassword" || (key == "password" && this.form.repassword != "")) {
                    if (this.form.repassword != this.form.password) {
                        this.formVali["repassword"] = "两次密码不一致";
                        this.formTag["repassword"] = false;
                    } else {
                        this.formVali["repassword"] = "";
                        this.formTag["repassword"] = true;
                    }
                } else {
                    var text = validateModule.done(this.form, key);

                    if (text != "") {
                        this.formTag[key] = false;
                    } else {
                        this.formTag[key] = true;
                    }
                    this.formVali[key] = text;
                }
            },
            addUser: function() {
                locationHerf("add_user", {
                    role: this.role,
                });
            },
            editUser: function(user) {
                locationHerf("edit_user", {
                    role: this.role,
                    user: user,
                });
            },
            deleteUser: function(user) {
                var self = this;
                swal({
                    title: "您确定要删除吗？",
                    type: "warning",
                    showCancelButton: true,
                    closeOnConfirm: false,
                    confirmButtonText: "是的，我要删除",
                    cancelButtonText: "取消",
                    confirmButtonColor: "#ec6c62"
                }, function(){
                    $.ajax({
                        type: 'POST',
                        url: "user.jsp",
                        data: hasHospitalPower.parms({
                            act: "delete_user",
                            user_id: user.id + "",
                        }),
                        success: function(res) {
                            res = JSON.parse(res);
                            if (res.errcode == 0) {
                                var index = self.users.indexOf(user);
                                if (index != -1) {
                                    self.users.splice(index, 1);
                                    swal("删除成功");
                                } else {
                                    swal("删除失败");
                                }
                            } else {
                                swal(res.errmsg);
                            }
                        }.bind(this),
                    });
                })
            },
            resetPassword: function() {
                if (!this.valiResultPass) {
                    swal("请正确输入密码");
                    return;
                }
                $("#myModal").modal("hide");
                $.ajax({
                    type: 'POST',
                    url: "user.jsp",
                    data: hasHospitalPower.parms({
                        act: "reset_password",
                        user_id: this.nowUser.id + "",
                        password: this.form.password,
                        repassword: this.form.repassword,
                    }),
                    success: function(res) {
                        res = JSON.parse(res);
                        if (res.errcode == 0) {
                            swal(res.errmsg);
                        } else {
                            swal(res.errmsg);
                        }
                    }.bind(this),
                });
            }
        },
        ready: function() {
            if (hasHospitalPower.power()) {
                this.title = "-" + hasHospitalPower.nowHospital.name;
            }
            validateModule.addValidate("password", "password");
            var userParms = getHrefParms("user");
            var role = this.role = userParms.role;
            this.roleName = role.name;
            $.ajax({
                type: 'POST',
                url: "user.jsp",
                data: hasHospitalPower.parms({
                    act: "users",
                    role_id: role.id + "",
                }),
                success: function(res) {
                    res = JSON.parse(res);
                    if (res.errcode == 0) {
                        this.users = res.data;
                    }
                }.bind(this),
            });
        }
    });
    $('.delete_doc_mes').click(function(){
        swal({
            title: "您确定要删除吗？",
            type: "warning",
            showCancelButton: true,
            closeOnConfirm: false,
            confirmButtonText: "是的，我要删除",
            cancelButtonText: "取消",
            confirmButtonColor: "#ec6c62"
        }, function(){

        })
     })
</script>

</body>
</html>
